package com.lkxicy.wos.service.impl;

import com.lkxicy.wos.mapper.HolidayMapper;
import com.lkxicy.wos.pojo.entity.Schedule;
import com.lkxicy.wos.mapper.ScheduleMapper;
import com.lkxicy.wos.pojo.vo.ScheduleRecord;
import com.lkxicy.wos.service.ScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-12-02
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private HolidayMapper holidayMapper;

    @Override
    public List<ScheduleRecord> findOneAllNowSchedule(String staffNumber) {
        return scheduleMapper.findOneAllNowSchedule(staffNumber);
    }

    @Override
    public List<ScheduleRecord> searchSomedaySchedule(Date searchTime) {
        return scheduleMapper.searchSomedaySchedule(searchTime);
    }

    @Override
    public String addSchedule(Date beginTime, Date endTime, List<String> staffNumbers, List<String> workContents) {
        boolean flag=false;
        // 这里因为工号集合和工作内容集合长度是一样的，所以循环其中一种就OK，或者用map来处理也可以
        for(int i=0;i<staffNumbers.size();i++){
            if(holidayMapper.findOneHolidayRecord(staffNumbers.get(i),beginTime,endTime)==null){
                return staffNumbers.get(i)+" 号员工还在假期内，排班失败，请重新排班";
            }

            // 还要加上一个判断  如果某个员工在这个时间段已经有工作了，就进行update而不是insert
            Schedule schedule = new Schedule();
            schedule.setStaffNumber(staffNumbers.get(i));
            schedule.setWorkContent(workContents.get(i));
            schedule.setBeginTime(beginTime);
            schedule.setEndTime(endTime);

            // 如果在排班的时候发现该员工的这一天已经有工作了   就把insert换成update
            Schedule sc=scheduleMapper.findOneSchedule(staffNumbers.get(i),beginTime);
            if(sc!=null){
                flag=true;
                schedule.setId(sc.getId());
                scheduleMapper.updateById(schedule);
            }
            else{
                scheduleMapper.insert(schedule);
            }
        }
        if(!flag){
            return "排班成功！";
        }
        else{
            return "排班成功！过程中对部分员工该天的工作重新安排，请核查！";
        }
    }

    @Override
    public String deleteSchedule(String staffNumber,Date workTime) {
        scheduleMapper.deleteSchedule(staffNumber, workTime);
        return "删除排班记录成功！";
    }

}
